package com.mlink.ai.chat.ui.adapter

import android.support.annotation.DrawableRes
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.ItemLangugeBinding

enum class LanguageItem(@DrawableRes val icon: Int, @StringRes val title: Int, val code: String) {
    ENGLISH_US(R.drawable.ic_america, R.string.language_america, "en,US"), // 美国
    ENGLISH_UK(R.drawable.ic_england, R.string.language_england, "en,GB"), // 英国
    CHINESE(R.drawable.ic_china, R.string.language_china, "zh,CN"), // 中国
    RUSSIAN(R.drawable.ic_russia, R.string.language_russia, "ru,RU"), // 俄罗斯
    SPANISH(R.drawable.ic_spain, R.string.language_spain, "es,ES"),// 西班牙
    GERMAN(R.drawable.ic_germany, R.string.language_germany, "de,DE"), // 德国
    JAPANESE(R.drawable.ic_japan, R.string.language_japan, "ja,JP"), //日本
    BRAZIL(R.drawable.ic_brazil, R.string.language_portugal, "pt,BR"),
    PORTUGUESE(R.drawable.ic_portugal, R.string.language_portugal, "pt,PT"), //葡萄牙
    FRENCH(R.drawable.ic_france, R.string.language_france, "fr,FR") //法国
}

class LanguageRecyclerAdapter : RecyclerView.Adapter<LanguageRecyclerAdapter.LanguageViewHolder>() {

    var data = mutableListOf<LanguageItem>().apply {
        add(LanguageItem.ENGLISH_US)
        add(LanguageItem.ENGLISH_UK)
        add(LanguageItem.SPANISH)
        add(LanguageItem.BRAZIL)
        add(LanguageItem.PORTUGUESE)
        add(LanguageItem.FRENCH)
        add(LanguageItem.RUSSIAN)
        add(LanguageItem.GERMAN)
        add(LanguageItem.CHINESE)
        add(LanguageItem.JAPANESE)
    }
    var selectedItem: LanguageItem? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LanguageViewHolder {
        return LanguageViewHolder(
            ItemLangugeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun onBindViewHolder(holder: LanguageViewHolder, position: Int) {
        val item = data[position]
        holder.binding.run {
            ivIcon.setImageResource(item.icon)
            tvTitle.text = root.context.getString(item.title)
            llRoot.setBackgroundResource(
                if (selectedItem == item) {
                    R.drawable.shape_bg_item_language_select
                } else {
                    R.drawable.shape_bg_item_language_unselect
                }
            )
            llRoot.setOnClickListener {
                val prePosition = data.indexOf(selectedItem)
                val afterPosition = data.indexOf(item)
                selectedItem = item
                notifyItemChanged(prePosition)
                notifyItemChanged(afterPosition)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class LanguageViewHolder(val binding: ItemLangugeBinding) :
        RecyclerView.ViewHolder(binding.root)
}